<%@page import="DB.DBProvider"%>
<%@ page language="java" contentType="text/html; charset=windows-1255"
    pageEncoding="windows-1255"%>
<%@ page import="java.util.*, java.io.*,java.sql.ResultSet" %>
<% 
  String admin = (String)request.getSession().getAttribute("administrator");
  if (null == admin){
	   response.sendError(HttpServletResponse.SC_INTERNAL_SERVER_ERROR);
  }
  String number = request.getParameter("number");
  
  int start = 0;
  
  if (null != number){
	   start = Integer.parseInt(number);
  }
%>
<script type="text/javascript">
	function del_user(user){
		var num = <%=start%>;
		$('#page').load("/Calendars/Homeservlet?op=deluser&number="+num+"&user="+user,function(response, status, xhr) {
			  if (status == "error") {
				    var msg = "Sorry but there was an error: ";
				    $("#page").html("<br />" +"<h1>" + msg + xhr.status + " " + xhr.statusText +"</h1>" );
				  }
				});
	}
	
	function show_user_time_table(user){
		$('#page').load("/Calendars/Web/timeTable.jsp?number=<%=start%>&requestedUser=" + user,function(response, status, xhr) {
			  if (status == "error") {
				    var msg = "Sorry but there was an error: ";
				    $("#page").html("<br />" +"<h1>" + msg + xhr.status + " " + xhr.statusText +"</h1>" );
				  }
				});
	}
	
	function see_users(number){
		var num = <%=start%> + number; 
		$('#page').load("/Calendars/Web/users.jsp?number=" + num,function(response, status, xhr) {
			  if (status == "error") {
				    var msg = "Sorry but there was an error: ";
				    $("#page").html("<br />" +"<h1>" + msg + xhr.status + " " + xhr.statusText +"</h1>" );
				  }
				});
	}
	</script>


<div class="post"">
<%   
   Properties p = new Properties();
   p.load(getServletContext().getResourceAsStream("/WEB-INF/config"));
   ResultSet res = (new DBProvider(p)).getUsersDetails(start);

   if (!res.next()){%>
   	`  <br/>
	   <h1 class="title"> There aren't any users to display</h1>
	   <%		if (start !=0){ %>
	   			<br/>
		<table ALIGN=RIGHT>
		<tr><td><button style="height: 125px; width: 100px"  onclick="see_users(-5)">See the previous 5 users</button></td></tr>
		</table>
		<% } %>
<% } else { 
	res.previous();
	%>
	<h1 class="title">Users</h1>
		<br/>
		<table ALIGN=RIGHT>
		<tr><td><button style="height: 125px; width: 100px" onclick="see_users(5)">See the next 5 users</button></td></tr>
<%		if (start !=0){ %>
		<tr><td><button style="height: 125px; width: 100px"  onclick="see_users(-5)">See the previous 5 users</button></td></tr>
	<%} %>
		</table>
		<br/>
		<table class = "usersTable">
			<tr>
			<th>Username</th>
			<th>First name</th>
			<th>Last name</th>
			<th>Phone number</th>
			<th>Administrator</th>
			<th></th>
			<th></th>
		</tr>
<%	
	while (res.next())
	{
		String username = res.getString("username");
		String first_name = res.getString("first_name");
		String last_name = res.getString("last_name");
		String phone_number = res.getString("phone_number");
		String administrator = res.getBoolean("administrator") ? "YES" : "NO";
%>			
		<tr>
			<td><%=username%></td>
			<td><%=first_name%></td>
			<td><%=last_name%></td>
			<td><%=phone_number%></td>
			<td><%=administrator%></td>
			<td><button onclick="show_user_time_table(<%="'" + username+ "'"%>)">See user time table</button></td>
			<td><button onclick="del_user(<%="'" + username+ "'"%>)">Delete user</button></td>
		</tr>
<%
	}
%>
		</table>
	</div>
<%
} 
%>	 